const { Sequelize } = require('sequelize')

const sequelize = new Sequelize('sequelize_demo01', 'root', 'root', {
  host: 'localhost',
  port: 3306,
  dialect: 'mysql',
  logging: (sql, { bind = [] }) => console.log(sql, '\nparams:', ...bind),
})

;(async () => {
  try {
    // 测试连接：必须数据库事先存在
    await sequelize.authenticate()
    console.log('Connection has been established successfully.')
  } catch (error) {
    console.error('Unable to connect to the database:', error)
  }
  await sequelize.sync({ alter: true })
})()

module.exports = {
  sequelize,
}
